<template>
  <div>
    <img src="../assets/img/shuhui.png" alt="" class="logo">
    <div class="form">
      <form action="" @submit.prevent="submit">
        <div>
          <my-input name="name" placeholder="用户名/邮箱" v-model="form.name" pattern="^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$">
            <i class="fa fa-user-o"></i>
          </my-input>
          <hr>
          <div>
            <my-input name="password" type="password" placeholder="密码" v-model="form.password" pattern="^[\w]{6,16}$">
              <i class="fa fa-lock"></i>
            </my-input>
          </div>
        </div>
        <button :disabled="dis" :class="{success:!dis}">登录</button>
      </form>
    </div>
    <div class="login-footer">
      <router-link to="/register">
        <span class="border-right">注册</span>
      </router-link>
      <router-link to="/forget">
        <span>忘记密码</span>
      </router-link>


    </div>
  </div>
</template>
<script>
  import MyInput from "../components/MyInput"
  import bus from "../bus.js"
  export default {
    data() {
      return {
        form: {
          name: "",
          password: ""
        },
        dis: true,
        show: true,
        user: {},
        errtext: ""
      }
    },
    components: {
      MyInput
    },
    mounted() {
      bus.$on('changes', this.mychange)
    },
    methods: {
      submit() {
        this.axios.post('http://u.ishuhui.com/login', this.form)
          .then(data => {
            // console.log(data)
            // console.log(data.data.errNo)
            
            
            
            if (data.data.data) {
              this.show = false
              this.user = data.data.data
              console.log(this.user)
              localStorage.setItem('userid', this.user.id)
              localStorage.setItem('userrss', this.user.rss)
            }else{
              this.errtext = data.data.errNo == 104 ? "密码错误":"帐号未注册"
              this.$message.error(this.errtext);
            }


            this.$emit('hide', this.show)
          })
          .catch(err => {
            console.log(err)
            // errNo 104  密码错误   103  帐号未注册
            this.show = true
          })
      },
      mychange() {
        if (this.form.name.length >= 4 && this.form.password.length >= 6) {
          this.dis = false
        } else {
          this.dis = true
        }
      }
    }

  }

</script>
<style scoped>
  .logo {
    width: 50%;
    display: block;
    margin: 50px auto;
  }
  
  .form {
    width: 100%;
    font-size: .4rem;
  }
  
  .form i {
    color: #ccc;
  }
  
  .form form>div {
    width: 100%;
    padding: 15px;
    background-color: white;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
  }
  
  hr {
    background-color: #ccc;
    margin: 10px 0;
  }
  
  .form button {
    width: 90%;
    height: 45px;
    display: block;
    margin: 20px auto;
    line-height: 45px;
    text-align: center;
    font-size: .4rem;
    border: 1px solid #ccc;
    border-radius: 5px;
  }
  
  .success {
    color: white;
    background-color: #ff4400;
  }
  
  .login-footer {
    width: 100%;
    text-align: center;
    position: absolute;
    bottom: 100px;
  }
  
  .login-footer span {
    padding: 0 30px;
    font-size: .35rem;
    color: #a39e9e;
  }
  
  .border-right {
    border-right: 1px solid #ccc;
  }

</style>
